#!/bin/bash
# Systematic compilation with error logging
# This script compiles each file individually and logs results

BASE="/home/linden/claude/zclaude/conversion-evaluation/04-cpp-generated"
LOGS="/home/linden/claude/zclaude/conversion-evaluation/05-compilation-results/build-logs"
SUCCESS="/home/linden/claude/zclaude/conversion-evaluation/05-compilation-results/successful-builds"

declare -i total=0
declare -i success=0
declare -i failed=0

compile_one() {
    local repo=$1
    local file=$2

    total=$((total + 1))
    echo "[$total] Compiling $repo/$file..."

    cd "$BASE/$repo"
    if g++ -std=c++20 -Wall -Wno-unknown-pragmas -c "$file" 2>&1 | tee "$LOGS/$repo/${file%.cpp}_compile.log"; then
        echo "  ✓ SUCCESS"
        success=$((success + 1))
        if [ -f "${file%.cpp}.o" ]; then
            cp "${file%.cpp}.o" "$SUCCESS/${repo}_${file%.cpp}.o"
        fi
        return 0
    else
        echo "  ✗ FAILED"
        failed=$((failed + 1))
        return 1
    fi
}

echo "========================================="
echo " C++ COMPILATION TEST SUITE"
echo "========================================="
echo ""

# Compile all files
compile_one "DotNetCSharpLinqByExample" "pet.cpp"
compile_one "DotNetCSharpLinqByExample" "joins.cpp"
compile_one "DelegateTransform" "delegate_transform.cpp"
compile_one "DelegateTransform" "delegate_transform_tests.cpp"
compile_one "RobloxAPI" "group.cpp"
compile_one "RobloxAPI" "group_role.cpp"
compile_one "RobloxAPI" "http_helper.cpp"
compile_one "RobloxAPI" "outfit.cpp"
compile_one "RobloxAPI" "search_api.cpp"
compile_one "C5" "array_list.cpp"
compile_one "C5" "hash_set.cpp"

echo ""
echo "========================================="
echo " SUMMARY"
echo "========================================="
echo "Total: $total"
echo "Success: $success"
echo "Failed: $failed"
echo "Success Rate: $(awk "BEGIN {printf \"%.1f%%\", ($success/$total)*100}")"
echo "========================================="
